package com.googlecode.afdbaccess.web.common.login;

import com.googlecode.afdbaccess.cache.TransactionBase;
import com.googlecode.afdbaccess.cache.exceptions.DataLayerException;
import com.googlecode.afdbaccess.cache.exceptions.DataLayerWrappedSqlException;
import com.googlecode.afdbaccess.cache.preparedstatement.PreparedStatementArgumentsSetterHelper;
import com.googlecode.afdbaccess.cache.resultconversion.exceptions.DataLayerWrappedResultSetConversionException;

class PasswordFetcher extends TransactedTask<String> {
    @Override
    protected String perform(TransactionBase t, String username) throws DataLayerWrappedSqlException, DataLayerWrappedResultSetConversionException, DataLayerException {
        String sql = "";
        sql += "SELECT\n";
        sql += "  Person.password\n";
        sql += "FROM\n";
        sql += "  Person,\n";
        sql += "  PersonIdentification,\n";
        sql += "  PersonIdentificationType\n";
        sql += "WHERE\n";
        sql += "  PersonIdentification.identification = ? AND\n";
        sql += "  PersonIdentification.Person_id = Person.Person_id AND\n";
        sql += "  PersonIdentification.PersonIdentificationType_id = PersonIdentificationType.PersonIdentificationType_id AND\n";
        sql += "  PersonIdentificationType.constantId = 'username' \n";

        PreparedStatementArgumentsSetterHelper setter = new PreparedStatementArgumentsSetterHelper();
        setter.addArgument(username.toLowerCase());
        
        return t.executeStringQuery(sql, setter, 0);
    }
}
